<?php

class OrderInvoice extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		date_default_timezone_set('Asia/Singapore');
		$this->Page->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " Administrator | Order Invoice";		
		if (!$this->IsPostBack)
		{
			$activeRecord = $this->getItem();
			if ($activeRecord instanceof OrderRecord)
			{
				$this->lblOrderNum->Text = $activeRecord->Num;
				$this->lblOrderDate->Text = date('d/m/Y',$activeRecord->CreateDate);
	
				$this->lblCustomer->Text = $activeRecord->SFullName."<br />";
				$this->lblCustomer->Text .= "Email: ".$activeRecord->SEmail."<br />";
				$this->lblCustomer->Text .= "Phone: ".$activeRecord->SPhone;
			
				$this->lblShipping->Text = $activeRecord->SFullName."<br />";
				$this->lblShipping->Text .= $activeRecord->SAddress."<br />";
				$this->lblShipping->Text .= $activeRecord->SCity."<br />";
				$this->lblShipping->Text .= Common::getCountryName($activeRecord->SCountryCode)." ".$activeRecord->SPostalCode."<br />";
	
				$sqlmap = $this->Application->Modules['sqlmap']->Client;
				$this->rptOrderItem->DataSource = $sqlmap->queryForList("GetOrderItemByOrderID", $activeRecord->ID);
				$this->rptOrderItem->DataBind();

				$this->lblPaymentTerm->Text = $activeRecord->PaymentMethod;
				$this->lblComments->Text = ($activeRecord->Comments != "") ? $activeRecord->Comments : "No instruction";
	
				$this->nfSubtotal->Value = $activeRecord->Subtotal;
				$this->memberDiscount->Value = "-" . Common::roundTo($activeRecord->CouponAmount);
				$this->nfShippingAmount->Value = $activeRecord->ShippingAmount;
				$this->nfTotal->Value = $activeRecord->TotalAmount;
			}
		}
	}

	protected function getItem()
	{
		if ($this->Request->Contains("id") && $this->Request->Contains("num"))
		{
			$activeRecord = OrderRecord::finder()->findByorder_idAndorder_num(TPropertyValue::ensureInteger($this->Request['id']), $this->Request['num']);
			if($activeRecord === null)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","order");
				$this->mainBox->Visible = false;
			}
			return $activeRecord;
		}
		else
		{
			$this->Notice->Type = AdminNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","order");
			$this->mainBox->Visible = false;
		}
	}
}

?>